package com.neusoft.tijiancms.service.impl;

import com.neusoft.tijiancms.dto.OrdersPageRequestDto;
import com.neusoft.tijiancms.dto.OrdersPageResponseDto;
import com.neusoft.tijiancms.mapper.OrdersMapper;
import com.neusoft.tijiancms.po.Orders;
import com.neusoft.tijiancms.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

	@Autowired
	private OrdersMapper ordersMapper;
	
	@Override
	public OrdersPageResponseDto listOrders(OrdersPageRequestDto requestDto) {
		OrdersPageResponseDto responseDto = new OrdersPageResponseDto();
		//1. 获取到总的记录数
		int totalRow = ordersMapper.getOrdersCount(requestDto);
		responseDto.setTotalRow(totalRow);
		if(totalRow == 0) {
			return responseDto;
		}
		//2. 计算总页数
		int totalPageNum = 0;
		if(totalRow % requestDto.getMaxPageNum() == 0) {
			totalPageNum = totalRow / requestDto.getMaxPageNum();
		}else {
			totalPageNum = totalRow / requestDto.getMaxPageNum() + 1;
		}
		responseDto.setTotalPageNum(totalPageNum);
		//3.计算上一页和下一页
		int pageNum = requestDto.getPageNum();
		if(pageNum > 1) {
			responseDto.setPreNum(pageNum - 1);
		}
		if(pageNum < totalPageNum) {
			responseDto.setNextNum(pageNum + 1);
		}
		//4. 计算从第几行开始查询
		requestDto.setBeginNum((pageNum - 1) * requestDto.getMaxPageNum());
		//5. 查询业务数据
		List<Orders> list = ordersMapper.listOrders(requestDto);
		//6. 将数据添加到OrdersPageResponseDto中
		responseDto.setPageNum(pageNum);
		responseDto.setMaxPageNum(requestDto.getMaxPageNum());
		responseDto.setList(list);
		return responseDto;
	}

	@Override
	public Orders getOrdersById(Integer orderId) {
		return ordersMapper.getOrdersById(orderId);
	}

	@Override
	public int updateOrdersState(Orders orders) {
		return ordersMapper.updateOrdersState(orders);
	}

}
